package com.wxzz.elearing.livetelecast.quartz;

import com.wxzz.elearning.common.constants.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class PlatformLivetelecastQuartz {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;



    /**
     * 每4小时执行一次 统计直播数据
     */
    //@Scheduled(cron = "0 0 */4 * * ?")
    //@Scheduled(cron = "*/10 * * * * ?")
    //每隔5秒执行一次：*/5 * * * * ?
    //@Scheduled(cron = "*/5 * * * * ?")
    public void liveStatisticsDataQuartz() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.LIVE_STATISTICS_DATA_QUARTZ, RedisConstants.LIVE_STATISTICS_DATA_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
    }
}
